#!/usr/bin/env python

from httputil import HttpUtil
from config import logger

class Template(HttpUtil):
  def __init__(self, prefixURL=None):
    HttpUtil.__init__(self, prefixURL)

  def listTemplates(self, templatefilter):
    command = "listTemplates"
    logger.debug("command=" + command)
    args = {"templatefilter": templatefilter}
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response


  def createTemplate(self, snapshotid, name, displaytext, ostypeid):
    command = "createTemplate"
    logger.debug("command=" + command)
    args = {
             "snapshotid": snapshotid,
             "name": name,
             "displaytext": displaytext,
             "ostypeid": ostypeid
           }
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response["template"]

  def updateTemplate(self, templateid, name, displaytext):
    command = "updateTemplate"
    logger.debug("command=" + command)
    args = {
             "id": templateid,
             "name": name,
             "displaytext": displaytext
           }
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response["template"]

  def deleteTemplate(self, templateid):
    command = "deleteTemplate"
    logger.debug("command=" + command)
    args = {"id": templateid}
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response

  def createServiceOffering(self, cpunumber, cpuspeed, memory, name, displaytext):
    command = "createServiceOffering"
    logger.debug("command=" + command)
    args = {
             "cpunumber": cpunumber,
             "cpuspeed": cpuspeed,
             "memory": memory,
             "name": name,
             "displaytext": displaytext
           }
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response["serviceoffering"]

  def listServiceOfferings(self):
    command = "listServiceOfferings"
    logger.debug("command=" + command)
    response = self.http_get(command)
    logger.debug(response)
    return response

  def updateServiceOffering(self, serviceofferingid, name, displaytext):
    command = "updateServiceOffering"
    logger.debug("command=" + command)
    args = {
             "id": serviceofferingid,
             "name": name,
             "displaytext": displaytext
           }
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response["serviceoffering"]

  def deleteServiceOffering(self, serviceofferingid):
    command = "deleteServiceOffering"
    logger.debug("command=" + command)
    args = {"id": serviceofferingid}
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response

  def createDiskOffering(self, name, displaytext, disksize, customized):
    command = "createDiskOffering"
    logger.debug("command=" + command)
    args = {
             "name": name,
             "displaytext": displaytext,
             "disksize": disksize,
             "customized": customized
           }
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response["diskoffering"]

  def listDiskOfferings(self):
    command = "listDiskOfferings"
    logger.debug("command=" + command)
    response = self.http_get(command)
    logger.debug(response)
    return response

  def updateDiskOffering(self, diskofferingid, name, displaytext):
    command = "updateDiskOffering"
    logger.debug("command=" + command)
    args = {
             "id": diskofferingid,
             "name": name,
             "displaytext": displaytext
           }
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response["diskoffering"]

  def deleteDiskOffering(self, diskofferingid):
    command = "deleteDiskOffering"
    logger.debug("command=" + command)
    args = {"id": diskofferingid}
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response

